package com.teng.smartdrawer.utils;

import android.content.Context;
import android.util.DisplayMetrics;

import com.teng.smartdrawer.MyApplication;
import com.teng.smartdrawer.bean.Seek;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.io.File;

/**
 * Created by teng on 16/2/19.
 */
public class Utils {

    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int dp2px(Context context, int dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return (int) ((dp * displayMetrics.density) + 0.5);
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static final DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
            .setDbName("smartDrawerDB.db")
            .setDbDir(new File(MyApplication.sdcard_base_path))// 不设置dbDir时, 默认存储在app的私有目录.
            .setDbVersion(4)
            .setDbOpenListener(new DbManager.DbOpenListener() {
                @Override
                public void onDbOpened(DbManager db) {
                    // 开启WAL, 对写入加速提升巨大
                    db.getDatabase().enableWriteAheadLogging();
                }
            })
            .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                @Override
                public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                    try {
                        db.addColumn(Seek.class , "seek_time");
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    // db.dropTable(...);
                    // ...
                    // or
                    // db.dropDb();
                }
            });

}
